﻿using UnityEngine;
using System.Collections;

public class MyCharacterController : MonoBehaviour
{
    private VoxelTerrain voxelTerrain;
    public Point3 gridPos { get { return Point3.Round(transform.position); } }

    private Vector3 currentMotion = Vector3.zero;
    private Vector3 currentTranslation = Vector3.zero;

    void Start()
    {
        voxelTerrain = VoxelTerrain.Instance;
    }

    void LateUpdate()
    {
        if (voxelTerrain.GetBlockAt(gridPos.x, gridPos.y - 1, gridPos.z) == Block.Air)
        {
            currentMotion += World.gravity * Time.deltaTime;
        }
        else
        {
            currentMotion = Vector3.zero; // TODO Rework
        }

        transform.position += currentTranslation + currentMotion * Time.deltaTime;
        currentTranslation = Vector3.zero;

        float factor = Mathf.Pow(0.95f, Time.deltaTime);

        currentMotion *= factor;
    }

    public void Move(Vector3 translation)
    {
        currentTranslation += translation;
    }
    public void AddMotion(Vector3 motion)
    {
        currentMotion += motion;
    }
}
